/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-15 7:33:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.base.dto.BaseTimeDTO;
import com.jbinfo.qingcaowan.common.CheckParamUtil;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.JSONResult;
import com.jbinfo.qingcaowan.studyManager.dto.CycleDetailCardDTO;
import com.jbinfo.qingcaowan.studyManager.dto.CycleDetailDTO;
import com.jbinfo.qingcaowan.studyManager.dto.NoteDTO;
import com.jbinfo.qingcaowan.studyManager.dto.RemarkDTO;
import com.jbinfo.qingcaowan.studyManager.entity.BookInfo;
import com.jbinfo.qingcaowan.studyManager.entity.CycleMain;
import com.jbinfo.qingcaowan.studyManager.entity.NoteInfo;
import com.jbinfo.qingcaowan.studyManager.entity.Remark;
import com.jbinfo.qingcaowan.studyManager.service.BookManagerService;
import com.jbinfo.qingcaowan.studyManager.service.CardManagerService;
import com.jbinfo.qingcaowan.studyManager.service.CycleMainService;
import com.jbinfo.qingcaowan.studyManager.service.ReadingNoteService;
import com.jbinfo.qingcaowan.studyManager.service.RemarkService;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.user.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 书籍controller
 * @author John Zhao
 */
/**
 * @author Administrator
 *
 */
@Controller
public class BookController {

	private static final Logger LOG = LoggerFactory.getLogger(BookController.class);

	@Resource
	private BookManagerService bookManagerService;
	
	@Resource
	private CycleMainService cycleMainService;
	
	@Resource
	private RemarkService remarkService;
	
	@Resource
	private ReadingNoteService readingNoteService;
	
	@Resource
	private CardManagerService cardManagerService;
	
	@Resource
	private UserService userService;

	// 获取阅读的书籍概论
	@RequestMapping(value = "/{client}/getBookGist")
	public @ResponseBody
	Object getNoticeCnt(@PathVariable("client") String client
			, UserDTO userDTO, HttpSession session, BookInfo bookInfo) {
		LOG.info("客户端为 " + client + " noticeDTO：" + bookInfo.toString());
		
//		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
//		
//		UserDTO userDTO = loginContext.getLoginUser();
//		String orgId = userDTO.getUserOrg();
		
		// 根据userId 查询orgId
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
		bookInfo.setOrgId(orgId);
		
		List<BookInfo> bookList = bookManagerService.getBookGist(orgId);
		
		return bookList;
	}
	
	// 获取学习进度及阅读的书籍概论
	@RequestMapping(value = "/{client}/getCycleAndBook")
	public @ResponseBody
	Object getCycleDate(@PathVariable("client") String client
			, UserDTO userDTO, HttpServletResponse response
			, CycleMain cycleMain) {
		
		LOG.info("客户端为 " + client );
//		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
//		
//		UserDTO userDTO = loginContext.getLoginUser();
//		String orgId = userDTO.getUserOrg();
		
		CheckParamUtil.checkFieldIfNotBlank(cycleMain, "queryDate");
		// 根据userId 查询orgId
		
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
		cycleMain.setOrgId(orgId);
		
		//CycleDetail cycleDetail = (CycleDetail) cycleMainService.findCycleDetail(cycleMain);
		
		return cycleMainService.findCycleDetail(cycleMain);
	}
	
	// 获取该书籍评论数
	@RequestMapping(value = "/{client}/getBookRemarkCnt")
	public @ResponseBody
	Object getBookRemarkCnt(@PathVariable("client") String client, HttpSession session, RemarkDTO remarkDTO) {
		
		LOG.info("客户端为 " + client + ", remarkDTO : " + remarkDTO.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(remarkDTO, "remarkBookid");
		
		Remark remark = ClonePojoUtil.copyClassFromTo(remarkDTO, Remark.class);
		
		JSONResult jsonResult = (JSONResult) remarkService.getRemarkCnt(remark);
		
		return jsonResult;
	}
	
	// 获取该书籍笔记数
	@RequestMapping(value = "/{client}/getBookNoteCnt")
	public @ResponseBody
	Object getBookNoteCnt(@PathVariable("client") String client, 
			HttpServletResponse response, NoteDTO noteDTO) {
		
		LOG.info("客户端为 " + client + ", noteDTO : " + noteDTO.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(noteDTO, "noteBookId");
		
		NoteInfo noteInfo = ClonePojoUtil.copyClassFromTo(noteDTO, NoteInfo.class);
		
		JSONResult jsonResult = (JSONResult) readingNoteService.getNoteCnt(noteInfo);
		
		return jsonResult;
	}
	
	// 分页查询笔记列表
	@RequestMapping(value = "/{client}/getBookNoteByPage")
	public @ResponseBody
	Object getBookNoteByPage(@PathVariable("client") String client, 
			HttpServletResponse response, NoteDTO noteDTO, 
			Integer currentPage, Integer pageSize) {
		
		LOG.info("客户端为 " + client + ", noteDTO : " + noteDTO.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(noteDTO, "userId");
		
		return readingNoteService.findNoteListByPage(noteDTO, currentPage, pageSize);
	}
	
	// 根据noteNum 查询笔记详情
	@RequestMapping(value = "/{client}/findNoteInfoByNum")
	public @ResponseBody
	Object findNoteInfoByNum(@PathVariable("client") String client, 
			NoteDTO noteDTO, HttpServletResponse response) {
		
		LOG.info("客户端为 " + client + ", noteDTO : " + noteDTO.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(noteDTO, "noteNum");
		
		return readingNoteService.findNoteByNum(noteDTO);
	}
	
	// 获取该书籍卡片数
	@RequestMapping(value = "/{client}/getCardCnt")
	public @ResponseBody
	Object getCardCnt(@PathVariable("client") String client
			, UserDTO userDTO, BookInfo bookInfo) {
		
		LOG.info("客户端为 " + client + ", bookInfo : " + bookInfo.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(bookInfo, "bookId");
		
		// 根据userId 查询orgId
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
//		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
//		
//		UserDTO userDTO = loginContext.getLoginUser();
//		String orgId = userDTO.getUserOrg();
		bookInfo.setOrgId(orgId);
		
		JSONResult jsonResult = (JSONResult) bookManagerService.getCardCnt(bookInfo);
		
		return jsonResult;
	}
	
	// 获取书籍的学习计划及卡片信息
	@RequestMapping(value = "/{client}/getCycleCard")
	public @ResponseBody
	Object getCycleCard(@PathVariable("client") String client, UserDTO userDTO, CycleMain cycleMain) {
		
		LOG.info("客户端为 " + client + ", cycleMain : " + cycleMain.toString() );
		
		CheckParamUtil.checkFieldIfNotBlank(cycleMain, "bookId", "queryDate");
		
		// 根据userId 查询orgId
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
		cycleMain.setOrgId(orgId);
		cycleMain.setUserId(userDTO.getUserId());
		
		return cardManagerService.findBookCycle(cycleMain);
	}
	
	
	// 获取书籍的学习计划(具体日期)
	@RequestMapping(value = "/{client}/getBookCycleDate")
	public @ResponseBody
	Object getBookCycleDate(@PathVariable("client") String client
			, UserDTO userDTO, CycleDetailDTO cycleDetailDTO
			, HttpServletResponse response) {
		LOG.info("客户端为 " + client + " cycleDetailDTO：" + cycleDetailDTO.toString());
		
		// 根据userId 查询orgId
//		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
//		UserDTO user = userJson.getResultBean();
//		String orgId = user.getUserOrg();
//		
//		cycleDetailDTO.setOrgId(orgId);
		
//		String bookId = cycleDetailDTO.getBookId();
		
		return cycleMainService.findCycleDetailById(cycleDetailDTO);
	}
	
	// 保存用户卡片阅读时间
	@RequestMapping(value = "/{client}/saveReadCardTime")
	public @ResponseBody
	Object saveReadCardTime(@PathVariable("client") String client
			, BaseTimeDTO baseTimeDTO
			, HttpServletResponse response) {
		LOG.info("客户端为 " + client + " baseTimeDTO：" + baseTimeDTO.toString());
		
		// 根据userId 查询orgId
		UserDTO userDTO = new UserDTO();
		userDTO.setUserId(baseTimeDTO.getUserId());
		
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
		baseTimeDTO.setId(UUIDUtils.create());
		baseTimeDTO.setOrgId(orgId);
//		baseTimeDTO.setTaskDay(taskDay);
		baseTimeDTO.setGmtCreate(new Date());
		
		return cycleMainService.saveReadCardTime(baseTimeDTO);
	}
	
	// 根据卡片顺序及任务日期获取卡片内容
	@RequestMapping(value = "/{client}/findCardByNumber")
	public @ResponseBody
	Object findCardByNumber(@PathVariable("client") String client
			, CycleDetailCardDTO cycleDetailCardDTO
			, HttpServletResponse response) {
		LOG.info("客户端为 " + client + " cycleDetailCardDTO：" + cycleDetailCardDTO.toString());
		
		CheckParamUtil.checkFieldIfNotBlank(cycleDetailCardDTO, "userId", "cardNumber","taskDate");
		
		return cardManagerService.getCardInfoByNumber(cycleDetailCardDTO);
	}
	
	// 根据  userId  查询学习计划中书籍列表
	@RequestMapping(value = "/{client}/getBookCycleList")
	public @ResponseBody
	Object getBookCycleList(@PathVariable("client") String client
			, UserDTO userDTO
			, HttpServletResponse response
			, Integer currentPage, Integer pageSize) {
		LOG.info("客户端为 " + client + " userDTO：" + userDTO.toString());
		
		// 根据userId 查询orgId
//			JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
//			UserDTO user = userJson.getResultBean();
//			String orgId = user.getUserOrg();
//			
//			cycleDetailDTO.setOrgId(orgId);
		
//			String bookId = cycleDetailDTO.getBookId();
		String userId = userDTO.getUserId();
		
		return cycleMainService.findCycleBookList(userId, currentPage, pageSize);
	}
	
	// 根据  userId  查询首页中的总阅读时间、总阅读本数、总阅读天数
	@RequestMapping(value = "/{client}/getTotalReadCycle")
	public @ResponseBody
	Object getTotalReadCycle(@PathVariable("client") String client
			, UserDTO userDTO
			, HttpServletResponse response) {
		
		LOG.info("客户端为 " + client + " userDTO：" + userDTO.toString());
		
		String userId = userDTO.getUserId();
		
		return cycleMainService.findTotalReadCycle(userId);
	}
}
